package com.honghu.villagepost.api;

import android.text.TextUtils;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPUtils;
import com.honghu.villagepost.constant.Constants;
import com.honghu.villagepost.util.MD5Util;
import com.honghu.villagepost.constant.Constants;
import com.honghu.villagepost.util.MD5Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by mqwang on 2017/11/17.
 */

public class BaseApi {


    /**
     * 统一签名
     * @param params
     * @return
     */
    public static String getSign(Map<String, Object> params){

        String token = SPUtils.getInstance().getString(Constants.TOKEN);
        if (!TextUtils.isEmpty(token)){
//            params.put(AUTHORIZATION, BEARER + token);
//            requestBuilder.addHeader();

        }
        params.put("singcode", DeviceUtils.getAndroidID());
        params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        List<Map.Entry<String, Object>> infoIds = new ArrayList<>(params.entrySet());
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>(){
            @Override
            public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2)
            {
                return (o1.getKey()).toString().compareTo(o2.getKey());
            }
        });
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> entry:infoIds) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        sb.append("key="+"7957Cc718Fd459A7bCc5eE039c3d0292");
        String str = sb.toString();
        String sign = MD5Util.encodeLowercaseHex(str);
        return sign;
    }



}
